package Operation;

import book.Book;
import book.Bookshelf;

import java.util.Scanner;

public class addition implements IoOperation {
    @Override
    public void work(Bookshelf bookshelf){
        int current = bookshelf.getNumber();
        if (current + 1 > bookshelf.getBookssize()){
            System.out.println("书架满了，不能添加！！");
            return;
        }


        System.out.println("正在添加图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = scanner.nextLine();

        System.out.println("请输入作者：");
        String author = scanner.nextLine();

        System.out.println("请输入价格：");
        int price = scanner.nextInt();

        System.out.println("请输入类型：");
        String type = scanner.nextLine();

        Book newbook = new Book(name,author,price,type);//通过将对象实例化，将输入的信息联系起来

        for (int i = 0; i < current; i++){
            Book book = bookshelf.getBooks(i);
            if (book.getName().equals(name)){
                System.out.println("书架已经有这本书了，添加失败");
                return;
            }
        }
        bookshelf.setBooks(current,newbook);
        bookshelf.setNumber(current + 1);
        System.out.println("添加图书成功！！");
    }
}
